package com.amumu.drama.common.modules.order.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.amumu.drama.common.exception.Asserts;
import com.amumu.drama.common.modules.order.dto.req.OrderConfigReq;
import com.amumu.drama.common.modules.order.mapping.SimpleOrderConfigReqMapping;
import com.amumu.drama.common.modules.order.service.OmsOrderSettingService;
import com.amumu.drama.common.modules.order.service.OrderConfigService;
import com.amumu.drama.mapper.OmsOrderSettingMapper;
import com.amumu.drama.model.OmsOrderSetting;
import com.amumu.drama.model.order.OrderConfig;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单设置管理Service实现类
 * Created by macro on 2018/10/16.
 */
@Slf4j
@Service
public class OmsOrderSettingServiceImpl implements OmsOrderSettingService {
    @Autowired
    private OmsOrderSettingMapper orderSettingMapper;
    @Autowired
    private OrderConfigService orderConfigService;

    @Override
    public OmsOrderSetting getItem() {
        Map<String, String> orderConfigMap = orderConfigService.list(null).stream()
                .filter(item -> StrUtil.isNotBlank(item.getConfigValue()))
                .collect(Collectors.toMap(OrderConfig::getConfigKey, OrderConfig::getConfigValue, (v1, v2) -> v2));
        OmsOrderSetting setting = new OmsOrderSetting();
        try {
            Field[] fields = ReflectUtil.getFields(OmsOrderSetting.class);
            for (Field field : fields) {
                String fieldName = field.getName();
                String fieldValue = orderConfigMap.get(fieldName);
                if (fieldValue != null) {
                    ReflectUtil.setFieldValue(setting, field, fieldValue);
                }
            }
        } catch (SecurityException e) {
            log.error("获取orderSetting异常：", e);
        }
        return setting;
    }

    @Override
    public boolean update(Long id, OmsOrderSetting orderSetting) {
        List<OrderConfig> orderConfigList = Lists.newArrayList();
        Field[] fields = ReflectUtil.getFields(OmsOrderSetting.class);
        for (Field field : fields) {
            String fieldName = field.getName();
            Object fieldValue = ReflectUtil.getFieldValue(orderSetting, field);
            OrderConfig orderConfig = new OrderConfig();
            orderConfig.setConfigKey(fieldName);
            if (ObjectUtil.isNotNull(fieldValue) && StrUtil.isNotBlank(fieldValue.toString())) {
                orderConfig.setConfigValue(StrUtil.toString(fieldValue));
            }
            ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
            orderConfig.setRemark(annotation.value());
            orderConfigList.add(orderConfig);
        }
        List<OrderConfig> saveOrUpdateList = orderConfigList.stream().filter(orderConfig -> StrUtil.isNotBlank(orderConfig.getConfigValue())).collect(Collectors.toList());
        orderConfigList.stream().filter(orderConfig -> StrUtil.isBlank(orderConfig.getConfigValue()))
                .forEach(orderConfig ->
                        orderConfigService.lambdaUpdate()
                                .set(OrderConfig::getConfigValue, null)
                                .set(OrderConfig::getRemark, orderConfig.getRemark())
                                .eq(OrderConfig::getConfigKey, orderConfig.getConfigKey())
                                .update());
        return orderConfigService.saveOrUpdateBatch(saveOrUpdateList);
    }

    @Override
    public OmsOrderSetting getOrderSetting() {
        List<OmsOrderSetting> omsOrderSettings = orderSettingMapper.selectList(null);
        if (CollUtil.isNotEmpty(omsOrderSettings)) {
            return omsOrderSettings.get(0);
        }
        return null;
    }

    /** 更新订单配置 */
    @Override
    public Boolean updateConfigList(OrderConfigReq request) {
        if (CollUtil.isEmpty(request.getOrderConfigList())) {
            Asserts.fail("更新配置列表为空");
        }
        List<OrderConfig> orderConfigList = SimpleOrderConfigReqMapping.INSTANCE.convertFrom(request.getOrderConfigList());
        List<OrderConfig> saveOrUpdateList = orderConfigList.stream().filter(orderConfig -> StrUtil.isNotBlank(orderConfig.getConfigValue())).collect(Collectors.toList());
        orderConfigList.stream().filter(orderConfig -> StrUtil.isBlank(orderConfig.getConfigValue()))
                .forEach(orderConfig ->
                        orderConfigService.lambdaUpdate()
                                .set(OrderConfig::getConfigValue, null)
                                .set(OrderConfig::getRemark, orderConfig.getRemark())
                                .eq(OrderConfig::getConfigKey, orderConfig.getConfigKey())
                                .update());
        return orderConfigService.saveOrUpdateBatch(saveOrUpdateList);
    }
}


